#include "stdafx.h"
#include "ZipParser.h"


//Create the Parser COM object from registration
void CZipParser::LoadOtherParser(CLSID classID)
{
	LPUNKNOWN lpUnk = NULL;
	HRESULT hRes = ::CoCreateInstance( classID, NULL, 
									CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER, 
									IID_IUnknown, 
									reinterpret_cast<void**>(&lpUnk) );
	if(SUCCEEDED(hRes))
	{
		hRes = lpUnk->QueryInterface(IID_ISPDocumentParser,reinterpret_cast<void**>(&m_OtherZipParser));
		lpUnk->Release();
	}

}

//Create the Parser COM object directly from the DLL
void CZipParser::LoadOtherParser(CLSID classID, CStringW dllPath)
{
	m_OtherZipParserDLL = ::LoadLibrary(dllPath.GetBuffer());
	if(m_OtherZipParserDLL)
	{
		UINT (CALLBACK* pfnGetClassObject) (REFCLSID, REFIID, LPVOID*);
		pfnGetClassObject = (UINT (CALLBACK*) (REFCLSID, REFIID, LPVOID*)) ::GetProcAddress(m_OtherZipParserDLL,"DllGetClassObject");
		if (pfnGetClassObject) 
		{
			IClassFactory* lpClassFactory;
			HRESULT hRes  = pfnGetClassObject(CLSID_SharePointDocumentParser, IID_IClassFactory, reinterpret_cast<void**>(&lpClassFactory));
			if(SUCCEEDED(hRes))
			{
				lpClassFactory->AddRef();
				LPUNKNOWN lpUnk=NULL;
				hRes = lpClassFactory->CreateInstance(NULL,IID_IUnknown,(LPVOID*)&lpUnk);
				if(SUCCEEDED(hRes))
				{
					hRes = lpUnk->QueryInterface(IID_ISPDocumentParser,reinterpret_cast<void**>(&m_OtherZipParser));
					lpUnk->Release();
				}
				lpClassFactory->Release();
			}
		}
	}
}
